﻿using SQLiteDB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZK.Entity;

namespace ZK.BLL
{
   public  class SeqenceBLL
    {

        private SeqenceBLL() { }
        /// <summary>
        /// 窗体对象实例
        /// </summary>
        private static SeqenceBLL _instance;
        public static SeqenceBLL Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new SeqenceBLL();
                return _instance;
            }
        }


        private Seqence GetEntity(string keyCode)
        {
            string sql = "Select * from Seqence Where KeyCode='" + keyCode + "'";
             return  SQLiteORM.Instance.GetEntity<Seqence>(sql);
        }

        private bool Update(Seqence seqence)
        {
          
            return SQLiteORM.Instance.Update<Seqence>(seqence, nameof(Seqence.KeyCode));
        }

        private bool Insert(Seqence seqence)
        {

            return SQLiteORM.Instance.Insert(seqence);
        }

        /// <summary>
        /// 流水号
        /// </summary>
        public  String GetSerialNumber(string keyCode )
        {
            var seqence = GetEntity(keyCode);
            if (seqence == null)
            {
                seqence = new Seqence() { KeyCode = keyCode, KeyVal = 1 };
                Insert(seqence);
                return seqence.KeyVal.ToString();
            }
            else {
                seqence.KeyVal++;
                Update(seqence);
                return seqence.KeyVal.ToString();
            }
        }

    }
}
